<?php


namespace app\index\controller;

use think\Db;
use think\exception\DbException;

/**
 * 数据库别名操作
 * Class DbAliasTest
 * @package app\index\controller
 */
class DbAliasTest
{

    /**
     * alias
     * sql：SELECT * FROM `t_user` `u` INNER JOIN `t_order` `t` ON `u`.`id`=`t`.`user_id`
     * @return \think\response\Json
     */
    public function alias_test()
    {
        try {
            $res = Db::table('t_user')
                ->alias('u')
                ->join('t_order t', 'u.id=t.user_id')
                ->select();
            return json(['msg' => '查询成功！', 'data' => $res]);
        } catch (DbException $e) {
            return json(['msg' => '查询失败！', 'data' => $e]);
        }
    }

    /**
     * alias
     * sql：SELECT * FROM `t_user` `u` INNER JOIN `t_order` `t` ON `u`.`id`=`t`.`user_id`
     * @return \think\response\Json
     */
    public function alias_test2()
    {
        try {
            $res = Db::table('t_user')
                ->alias(['t_user' => 'u', 't_order' => 't'])
                ->join('t_order', 'u.id=t.user_id')
                ->select();
            return json(['msg' => '查询成功！', 'data' => $res]);
        } catch (DbException $e) {
            return json(['msg' => '查询失败！', 'data' => $e]);
        }
    }

}